/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-06-21 09:22:13
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-06-21 16:17:51
 * @Description:
 */
import Watcher from './observer/watcher'
import { patch } from './vdom/patch'
export function lifeCycleMixin(Vue) {
	Vue.prototype._update = function (vnode) {
		const vm = this;
		vm.$el = patch(vm.$el, vnode)
	}
}

export function mountComponent(vm, el) {
	// vue 渲染过程中，会创建一个watcher，只要来渲染
	callHook(vm, 'beforeMount');
	// 更新回掉函数，每次渲染优先走这里
	const updateComponent = () => {
		// 通过调用render 创建虚拟dom Vnode
		// 通过update 将虚拟节点转换成真实节点
		vm._update(vm._render());
	}
	new Watcher(vm, updateComponent, () => { }, true);

	callHook(vm, 'mounted');

}
/**
 * 执行生命周期函数
 * @param {*} vm 
 * @param {*} hook 
 */

export function callHook(vm, hook) {
	let handlers = vm.$options[hook];
	if (handlers) {
		handlers.forEach(handler => {
			handler.call(vm);
		})
	}
}
